<?php
namespace app\index\model;
use think\Model;
class Indent extends Model
{
    public function getAuditAttr($value)
    {
        $audit = [0 => '退款成功', 1 => '请求退款', 2 => '买家可取货', 3 => '卖家提货中',4=>'交易完成',5=>'待付款'];
        return $audit[$value];
    }

    public function index($a,$b)
    {
        $audit=[$a,$b];
        $data = model('indent')
            ->alias('ind')
            ->join('issue', 'ind.issueid=issue.id', 'left')
            ->join('user', 'ind.userid=user.id', 'left')
            ->join('agency', 'ind.agencyid=agency.id', 'left')
            ->where('ind.audit','in',$audit)
            ->field('ind.*,issue.name,user.name,agency.name,issue.name as iname,user.name as uname,reality')
            ->order('audit desc')
            ->order('times desc')
            ->select();
        return $data;
    }

    public function modindent()
    {
        $ind = new Indent;
        if (isset($_POST['sub'])) {
            return $rs = $ind->where('id', input('hid'))
                ->update
                ([
                    'number' => input('number'),
                    'audit' => input('audit'),
                ]);
        }
    }
    public function modindent1($hid)
    {
        $ind = new Indent;
        return $rs = $ind->where('id',$hid)
        ->update(['audit' =>'0'] );
    }
    public function modindent2($hid)
    {
        $ind = new Indent;
        return $rs = $ind->where('id',$hid)
            ->update(['window' =>'1'] );
    }
    //收款单管理（完成交易的订单信息）
    public function StartData()
    {
        $res=$this->alias('ind')
            ->join('issue', 'ind.issueid=issue.id', 'left')
            ->join('user', 'ind.userid=user.id', 'left')
            ->join('agency', 'ind.agencyid=agency.id', 'left')
            ->where('ind.audit',"4")
            ->field('ind.*,issue.name,user.name,agency.name,issue.name as iname,user.name as uname')
            ->select();
        return $res;
    }
    //按品牌统计订单
    public function Tbrand(){
        $res=$this->alias('a')
            ->join('issue','a.issueid=issue.id','left')
            ->join('brand','brand.id=issue.brandid','left')
            ->group("brand.name")
            ->field('brand.name,count(brand.name) as count,a.id')
            ->select();
    }
    //总订单数
    public function zong(){
        $res=$this->field('count(id) as count ')->find();
        return $res['count'];
    }
    //进行中的订单数
    public function march(){
        $res=$this->where(['audit'=>['<>',0]])->field('count(id) as count ')->find();
        return $res['count'];
    }
    //退款订单数
    public function refund(){
        $res=$this->where('audit',0)->field('count(id) as count ')->find();
        return $res['count'];
    }
    //完成订单总钱数
    public function achieve(){
        $res=$this->where('audit',4)->field('sum(reality) as count ')->find();
        return $res['count'];
    }
    //获取今天的成交额
    public function todays(){
        $res=$this->where('audit',4)->whereTime('uptimes', 'today')->field('sum(reality) as count ')->find();
        return $res['count'];

    }
    //获取最近一周的成交额
    public function weeks(){
        $res=$this->where('audit',4)->whereTime('uptimes', 'week')->field('sum(reality) as count ')->find();
        return $res['count'];
    }
    //获取最近一月的成交额
    public function months(){
        $res=$this->where('audit',4)->whereTime('uptimes', 'month')->field('sum(reality) as count ')->find();
        return $res['count'];
    }
    //获取今天的下单数
    public function todaynumber(){
        $res=$this->whereTime('times', 'today')->field('count(id) as count ')->find();
        return $res['count'];

    }
    //获取最近一周的下单数
    public function weeknumber(){
        $res=$this->whereTime('times', 'week')->field('count(id) as count ')->find();
        return $res['count'];
    }
    //获取最近一月的下单数
    public function monthnumber(){
        $res=$this->whereTime('times', 'month')->field('count(id) as count ')->find();
        return $res['count'];
    }
}
